import cv2
import numpy as np

original = cv2.imread("img/CSDN_NOISE.jpg", cv2.IMREAD_UNCHANGED)

kernal = np.ones((5, 5), np.uint8)
# 先膨胀
result = cv2.dilate(original, kernal)
# 再腐蚀
result = cv2.erode(result, kernal)

# sobel算子边缘检测
sobelx = cv2.Sobel(result, cv2.CV_64F, 1, 0)
sobely = cv2.Sobel(result, cv2.CV_64F, 0, 1)
sobelx = cv2.convertScaleAbs(sobelx)
sobely = cv2.convertScaleAbs(sobely)
sobelxy = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
cv2.imshow("original", original)
cv2.imshow("result", result)
# cv2.imshow("x", sobelx)
# cv2.imshow("y", sobely)
cv2.imshow("xy", sobelxy)
cv2.waitKey()
cv2.destroyAllWindows()
